<template>
  <div class="MatchHome">
    <el-form ref="form" :model="form" label-width="80px" class="form">
      <div class="keyword">
        <el-form-item label="关键字搜索" label-width="100px">
          <el-input v-model="form.name" @keyup.enter.native="onEnterSubmit" placeholder="请输入关键字搜索" clearable></el-input>
        </el-form-item>
        <el-button style="margin-left: 20px;" size="mini" type="primary" @click.prevent="add_dialogVisible = true">添加关键字</el-button>
      </div>
    </el-form>
    <el-table v-loading="loading" :data="MatchShowList" class="MatchTable" stripe :default-sort="{ prop: 'ID', order: 'descending' }">
      <el-table-column label="ID" prop="id" width="60" sortable></el-table-column>
      <el-table-column label="关键字" prop="keyword" width="240"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click.prevent="handleEdit(scope.row.id, scope.row.keyword, scope.$index)">编辑</el-button>
          <el-button size="mini" type="danger" @click.prevent="handleDel(scope.row.id, scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="PagesAll"
      @current-change="ChangePage"
      style=" float: right;margin-top: 10px; margin-bottom: 40px; margin-right: 150px;"
    ></el-pagination>

    <!-- 编辑弹出层 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-input v-model="keyWord" placeholder="请输入内容"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="keyWordSubmit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加弹出层 -->
    <el-dialog title="添加关键字(至少两个汉字长度)" :visible.sync="add_dialogVisible" width="30%">
      <div class="add_keyWord_list">
        <span class="add_keyWord" v-for="(item, index) in keyWordArr" :key="index">{{ item }}</span>
      </div>
      <div class="keyWord_input">
        <el-input v-model="add_keyWord" placeholder="请输入内容"></el-input>
        <i class="el-icon-circle-plus-outline keyWord_input_i" @click="add_button"></i>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="add_dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAdd_submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  data() {
    return {
      form: {
        name: '',
        type: ''
      },
      PagesAll: null,
      MatchShowList: [],
      currentPage: 1,
      dialogVisible: false,
      add_dialogVisible: false,
      keyWord: '',
      add_keyWord: '',
      keyWordId: null,
      keyWordIndex: null,
      keyWordArr: []
    };
  },
  created() {
    this.get_allclub();
  },
  methods: {
    onEnterSubmit() {
      this.get_allclub();
      return false;
    },
    get_allclub() {
      this.loading = true;
      this.$axios
        .post(
          '/keyWord/index',
          {
            search: this.form.name,
            page: this.currentPage
          },
          {
            headers: {
              token: window.sessionStorage.getItem('token')
            }
          }
        )
        .then(res => {
          if (res.data.code == 2000) {
            this.MatchShowList = res.data.data.list;
            this.PagesAll = res.data.data.count;
            this.loading = false;
          } else {
            this.$message({
              type: 'error',
              message: res.data.message
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    ChangePage(page) {
      this.currentPage = page;
      this.get_allclub();
    },
    handleDel(id, index) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .post(
              '/keyWord/del_keyword',
              {
                id: id
              },
              {
                headers: {
                  token: window.sessionStorage.getItem('token')
                }
              }
            )
            .then(res => {
              if (res.data.code == 2000) {
                // this.MatchShowList.splice(index, 1);
                this.get_allclub();
                this.$message({
                  type: 'success',
                  message: res.data.msg
                });
              } else {
                this.$message({
                  type: 'error',
                  message: res.data.msg
                });
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    handleEdit(id, keyword, index) {
      this.keyWordIndex = index;
      this.keyWordId = id;
      this.keyWord = keyword;
      this.dialogVisible = true;
    },
    keyWordSubmit() {
      this.dialogVisible = false;
      this.$axios
        .post(
          '/keyWord/edit_keyword_post',
          {
            id: this.keyWordId,
            keyword: this.keyWord
          },
          {
            headers: {
              token: window.sessionStorage.getItem('token')
            }
          }
        )
        .then(res => {
          let { code, data } = res.data;
          if (code == 2000) {
            this.MatchShowList[this.keyWordIndex].keyword = data.keyword;
            this.$message({
              type: 'success',
              message: res.data.msg
            });
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    handleAdd_submit() {
      if(this.add_keyWord!=""){
        this.keyWordArr.push(this.add_keyWord)
      }
      if(this.keyWordArr.length ===0 ){
         this.$message({
              type: 'error',
              message: "内容不能为空！",
            });
        return;
      }
      this.$axios
        .post(
          '/keyWord/add_keyword_post',
          {
            keyword: this.keyWordArr
          },
          {
            headers: {
              token: window.sessionStorage.getItem('token')
            }
          }
        )
        .then(res => {
          let { code, data } = res.data;
          if (code == 2000) {
            this.get_allclub();
            this.$message({
              type: 'success',
              message: res.data.msg
            });
            this.add_dialogVisible = false;
          } else {
            this.$message({
              type: 'error',
              message: res.data.msg
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    add_button() {
      if (this.add_keyWord==="") return
      this.keyWordArr.push(this.add_keyWord);
      this.add_keyWord = ''
    }
  }
};
</script>

<style scoped>
.add_keyWord_list {
  display: flex;
  flex-flow: wrap;
}
.keyWord_input {
  display: flex;
  margin-top: 10px;
}
.keyWord_input_i {
  font-size: 41px;
  color: #dcdfe6;
  margin-left: 20px;
}
.add_keyWord {
  margin: 10px;
}
.form {
  display: flex;
  margin-left: 26px;
  margin-top: 50px;
  margin-bottom: 20px;
}
.state {
  width: 200px;
}
.keyword {
  display: flex;
  width: 410px;
}
.keyword .el-form-item {
  margin-bottom: 0;
}
.search {
  width: 123px;
  text-align: right;
}
.MatchTable {
  width: 95%;
  margin-top: 0px;
  margin-left: 50px;
  margin-bottom: 50px;
}
</style>
